//
//  ST_AIRecommendAdvanceFIlterController.swift
//  btvideo
//
//  Created by mac on 2020/7/9.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit
import SnapKit
import RealmSwift
import SwifterSwift



class ST_AIRecommendAdvanceFIlterController: UIViewController {
    
    
    private var current_filter: ST_PinterestHomeFilterModel
    
    init(filterModel: ST_PinterestHomeFilterModel?) {
        if let filterModel = filterModel{
            current_filter = filterModel
        }else{
            current_filter = ST_PinterestHomeFilterModel()
        }
        super.init(nibName: nil, bundle: nil)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        view.backgroundColor = .clear
        
        view.addSubview(root_view)
        root_view.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.bottom.equalToSuperview().offset(16)
            make.top.equalToSuperview().offset(139)
        }
        
        view.addSubview(top_view)
        top_view.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.bottom.equalTo(root_view.snp.top)
            make.top.equalToSuperview()
        }
        
        root_view.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(20)
            make.height.equalTo(22)
        }
        
        root_view.addSubview(reset_btn)
        reset_btn.snp.makeConstraints { (make) in
            make.height.equalTo(44)
            make.right.equalToSuperview().offset(-15)
            make.centerY.equalTo(titleLabel)
        }
        
        root_view.addSubview(root_scroll_view)
        root_scroll_view.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.bottom.equalToSuperview().offset(-128+34-BTSafeAreaBottom)
            make.top.equalTo(titleLabel.snp.bottom).offset(10)
        }
        
        
        root_view.addSubview(confirm_btn)
        confirm_btn.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.width.equalTo(260)
            make.height.equalTo(44)
            make.bottom.equalToSuperview().offset(-BTSafeAreaBottom+34-54)
        }
        
        
        root_scroll_view.addSubview(contentView)
        contentView.snp.makeConstraints { (make) in
            make.top.left.bottom.right.equalToSuperview()
            make.width.equalTo(ScreenWidth)
            make.height.equalTo(600+70+60)
        }
        
        
        
        
        contentView.addSubview(sub_base_label)
        sub_base_label.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(25)
            make.top.equalToSuperview().offset(10)
        }
        
        
        contentView.addSubview(genderItem)
        genderItem.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.top.equalTo(sub_base_label.snp.bottom).offset(5)
            make.height.equalTo(60)
        }
        
        contentView.addSubview(face_detected_item)
        face_detected_item.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.top.equalTo(genderItem.snp.bottom).offset(5)
            make.height.equalTo(60)
        }
        
        
        contentView.addSubview(distanceItem)
        distanceItem.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.top.equalTo(face_detected_item.snp.bottom).offset(5)
            make.height.equalTo(92)
        }
        
        
        contentView.addSubview(ageItem)
        ageItem.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.top.equalTo(distanceItem.snp.bottom)
            make.height.equalTo(92)
        }
        
        
        
        contentView.addSubview(advanced_label)
        advanced_label.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(25)
            make.top.equalTo(ageItem.snp.bottom).offset(16)
        }
        
        
        contentView.addSubview(seekItem)
        seekItem.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.top.equalTo(advanced_label.snp.bottom).offset(5)
            make.height.equalTo(60)
        }

        
        
        contentView.addSubview(statureItem)
        statureItem.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.top.equalTo(seekItem.snp.bottom)
            make.height.equalTo(60)
        }
        
        
        contentView.addSubview(ST_ZodiacItem)
        ST_ZodiacItem.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.top.equalTo(statureItem.snp.bottom)
            make.height.equalTo(60)
        }
        
        contentView.addSubview(EducationItem)
        EducationItem.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.top.equalTo(ST_ZodiacItem.snp.bottom)
            make.height.equalTo(60)
        }
        
        contentView.addSubview(careerItem)
        careerItem.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.top.equalTo(EducationItem.snp.bottom)
            make.height.equalTo(60)
        }
        
        
        bing_default_filter()
    }
    
    
    
    
    
    func bing_default_filter()  {
        
        distanceItem.bind(option: current_filter.distance)
        
        genderItem.bind(model: self.current_filter.genderFilter)
        
        face_detected_item.bind(model: self.current_filter.faceDetected)
        
        ageItem.bind(option: self.current_filter.age)
        
        statureItem.bind(model: current_filter.stature)
        
        ST_ZodiacItem.bind(model: self.current_filter.ST_Zodiac)
        
        EducationItem.bind(model: self.current_filter.Education)
        
        careerItem.bind(model: self.current_filter.career)
        
        seekItem.bind(model: self.current_filter.seek)
        
    }
    
//    MARK: - Properties
    
    fileprivate lazy var root_view:UIView = {
        let control = UIView()
        control.backgroundColor = Color.white
        control.cornerRadius = 16
        return control
    }()
    
    fileprivate lazy var top_view:UIView = {
        let control = UIView()
        control.backgroundColor = Color.clear
        let tap = UITapGestureRecognizer.init(target: self, action: #selector(close_click_handle))
        control.addGestureRecognizer(tap)
        return control
    }()
    
    fileprivate lazy var titleLabel:UILabel = {
        let control = UILabel.init()
        control.text = "搜索条件"
        control.textColor = Color.init(hexString: "#333333")
//        control.numberOfLines = 0
        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 18, weight: UIFont.Weight.semibold)
        return control
    }()
    
    fileprivate lazy var reset_btn:UIButton = {
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.setTitleForAllStates("清除选择")
        control.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.regular)
        control.setTitleColorForAllStates(UIColor.init(hexString: "#999999")!)
        control.addTarget(self, action: #selector(reset_handle), for: UIControl.Event.touchUpInside)
        return control
    }()
    
    fileprivate lazy var root_scroll_view:UIScrollView = {
        let control = UIScrollView()
//        control.backgroundColor = Color.random
        return control
    }()
    
    fileprivate lazy var contentView:UIView = {
        let control = UIView()
        control.backgroundColor = Color.white
        return control
    }()
    
    
    
    fileprivate lazy var confirm_btn:UIButton = {
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.setTitleForAllStates("确定")
        control.titleLabel?.font = UIFont.systemFont(ofSize: 16, weight: UIFont.Weight.semibold)
        control.setTitleColorForAllStates(UIColor.white)
        control.cornerRadius = 22
        control.backgroundColor = UIColor.init(hex: 0xFEC207)
        control.addTarget(self, action: #selector(confirm_handle), for: UIControl.Event.touchUpInside)
        return control
    }()
    
    
    
    fileprivate lazy var sub_base_label:UILabel = {
        let label = UILabel()
        let attrString = NSMutableAttributedString(string: "基础选项")
        label.frame = CGRect(x: 25, y: 188, width: 28, height: 20)
        label.numberOfLines = 0
        let attr: [NSAttributedString.Key : Any] = [.font: UIFont.systemFont(ofSize: 14),.foregroundColor: UIColor(red: 0.6, green: 0.6, blue: 0.6, alpha: 1)]
        attrString.addAttributes(attr, range: NSRange(location: 0, length: attrString.length))
        label.attributedText = attrString
        return label
    }()
    
    fileprivate lazy var ageItem:ST_FilterRangeSliderItemView = {
        
        let control = ST_FilterRangeSliderItemView()
        control.didChange = {[weak self] min,max in
           
            self?.current_filter.age.minValue = min
            self?.current_filter.age.maxValue = max
            
        }
        return control
    }()
    
    
    fileprivate lazy var genderItem:ST_PinterestFilterOptionView = {
        
        let control = ST_PinterestFilterOptionView()
        
        control.pushOptionClosure = {[weak self] m in
            
            guard let strongSelf = self,let gender = m.selectedId?.int else{
                return
            }
            
            let optionsController = ST_GenderOptionsController()
            
            optionsController.last_selected_gender = gender
            
            optionsController.selected_closure = { selected in
                
                
                let m = PinterestFilterOptionModel()

                m.keyString = "对你显示"
                
                if selected == 2{
                    m.value = "女"
                    m.selectedId = "2"
                }else{
                    m.value = "男"
                    m.selectedId = "1"
                }
                
                strongSelf.current_filter.genderFilter = m
                
                strongSelf.bing_default_filter()
                
            }
            
            strongSelf.navigationController?.pushViewController(optionsController)
            
        }
        return control
    }()
    
    
    fileprivate lazy var face_detected_item:PinterestFilterSwitchControl = {
        let control = PinterestFilterSwitchControl()
        control.pushOptionClosure = {[weak self] value in
            self?.current_filter.faceDetected = value
            self?.bing_default_filter()
        }
        return control
    }()
    
    fileprivate lazy var distanceItem:ST_FilterSliderItemView = {
        let control = ST_FilterSliderItemView()
        control.didChange = {[weak self] value in
            self?.current_filter.distance.value = value.string
        }
        return control
    }()
    
    
    
    fileprivate lazy var advanced_label:UILabel = {
        let label = UILabel()
        let attrString = NSMutableAttributedString(string: "个人资料")
        label.frame = CGRect(x: 25, y: 188, width: 28, height: 20)
        label.numberOfLines = 0
        let attr: [NSAttributedString.Key : Any] = [.font: UIFont.systemFont(ofSize: 14),.foregroundColor: UIColor(red: 0.6, green: 0.6, blue: 0.6, alpha: 1)]
        attrString.addAttributes(attr, range: NSRange(location: 0, length: attrString.length))
        label.attributedText = attrString
        return label
    }()
    
    
    
   fileprivate lazy var statureItem:PinterestStatureFilterOptionView = {
       
       let control = PinterestStatureFilterOptionView()
       control.pushOptionClosure = {[weak self] m in

        guard let strongSelf = self else{
            return
        }
        
        let minIdx = ST_BTStaturePickerView.statureOptions.firstIndex { (t) -> Bool in
            return t == strongSelf.current_filter.stature.minValue
        }
        let maxIdx = ST_BTStaturePickerView.statureOptions.firstIndex { (t) -> Bool in
            return t == strongSelf.current_filter.stature.maxValue
        }
        
        if strongSelf.stature_picker_view.superview == nil {
            
            strongSelf.view.addSubview(strongSelf.stature_picker_view)
            strongSelf.stature_picker_view.snp.makeConstraints { (make) in
                make.left.bottom.right.equalTo(strongSelf.view)
                make.height.equalTo(ST_BTTwicePickerView.height)
            }
        }
        
        
        
        if let section = minIdx,let row = maxIdx{
            
            print("当前选中的，",IndexPath.init(row: row, section: section))
            
            strongSelf.stature_picker_view.scrollTo(indexPath: IndexPath.init(row: row, section: section))
        }
        
        
        
        
        strongSelf.stature_picker_view.isHidden = false
        
        
        
        
       }
       return control
   }()
    
    
    fileprivate lazy var ST_ZodiacItem:ST_PinterestFilterOptionView = {
        
        let control = ST_PinterestFilterOptionView()
        control.pushOptionClosure = {[weak self] m in
            guard let strongSelf = self else{
                return
            }
            
            let optionsController = ST_PinterestHomeFilterOptionsController()
            
            optionsController.last_selected_option = m
            
            optionsController.selected_closure = { selected in
                
                strongSelf.current_filter.ST_Zodiac = selected
                
                strongSelf.bing_default_filter()
                
            }
            
            strongSelf.navigationController?.pushViewController(optionsController)
            
        }
        return control
    }()
    
    
    fileprivate lazy var EducationItem:ST_PinterestFilterOptionView = {
        
        let control = ST_PinterestFilterOptionView()
        control.pushOptionClosure = {[weak self] m in
            guard let strongSelf = self else{
                return
            }
            
            let optionsController = ST_PinterestHomeFilterOptionsController()
            
            optionsController.last_selected_option = m
            
            optionsController.selected_closure = { selected in
                
                strongSelf.current_filter.Education = selected
                
                strongSelf.bing_default_filter()
                
            }
            
            strongSelf.navigationController?.pushViewController(optionsController)
            
        }
        return control
    }()
    
    
    fileprivate lazy var careerItem:ST_PinterestFilterOptionView = {
        
        let control = ST_PinterestFilterOptionView()
        control.pushOptionClosure = {[weak self] m in
            guard let strongSelf = self else{
                return
            }
            
            let optionsController = ST_PinterestHomeFilterOptionsController()
            
            optionsController.last_selected_option = m
            
            optionsController.selected_closure = { selected in
                
                strongSelf.current_filter.career = selected
                
                strongSelf.bing_default_filter()
                
            }
            
            strongSelf.navigationController?.pushViewController(optionsController)
            
        }
        return control
    }()
    
    fileprivate lazy var seekItem:ST_PinterestFilterOptionView = {
        
        let control = ST_PinterestFilterOptionView()
        control.pushOptionClosure = {[weak self] m in
            guard let strongSelf = self else{
                return
            }
            
            let optionsController = ST_PinterestHomeFilterOptionsController()
            
            optionsController.last_selected_option = m
            
            optionsController.selected_closure = { selected in
                
                strongSelf.current_filter.seek = selected
                
                strongSelf.bing_default_filter()
                
            }
            
            strongSelf.navigationController?.pushViewController(optionsController)
            
        }
        return control
    }()
    
    fileprivate lazy var stature_picker_view:ST_BTStaturePickerView = {
            
       let control = ST_BTStaturePickerView()
        
       control.confirm_pick_closure = {[weak self] title,subTitle,indexPath in
        
        guard let strongSelf = self else{return}
        
        guard var selectedArray = [ST_BTStaturePickerView.statureOptions[safe:indexPath.section],ST_BTStaturePickerView.statureOptions[safe:indexPath.row]] as? [Int] else{
            return
        }
        
        selectedArray.sort()
        
        guard let min = selectedArray.first,let max = selectedArray[safe:1] else{
            return
        }
        
        strongSelf.current_filter.stature.minValue = min
        
        strongSelf.current_filter.stature.maxValue = max
        
        strongSelf.bing_default_filter()
        
       }
        
        return control
    }()
    
    
//    MARK: - Event
    @objc func close_click_handle(){
        
        goback_previous_controller()
        
    }
    
    @objc func reset_handle(){
        
        self.current_filter = ST_PinterestHomeFilterModel()
        
        bing_default_filter()
    }
    
    var refresh_closure:((ST_PinterestHomeFilterModel)->())?
    
    
    fileprivate func require_member_toast(){
        
        ST_MaidWayManager.shared.source = .偏好设置
        
        let vc  = ST_MemberSubscriberController.init(subscribeType: ST_BitianSubscribeType.筛选最想看的人)

        vc.overCurrentContext = true

        GlobalPresent(toController: vc)
        
    }
    
    @objc func confirm_handle(){
        
        if FETCH_MEMBER_STATUS() != .subscribe && ST_BTUserInfoManager.shared.additional_attributes?.searchNeedVip == true{
             
            require_member_toast()
            
            return
        }
        
        refresh_closure?(self.current_filter)
        
        goback_previous_controller()
        
    }
    
}
